package com.go.where.manager.action.go;

import javax.annotation.Resource;

import org.apache.log4j.Logger;

import com.go.where.manager.action.SuperAction;
import com.go.where.manager.common.Constants;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.CustomerInfo;
import com.go.where.manager.service.CustomerService;
import com.go.where.manager.util.CommonUtil;
import com.google.gson.Gson;

public class CustomerAction extends SuperAction {

	private static final long serialVersionUID = 1L;
	private static final Logger log = Logger.getLogger(CustomerAction.class);
	@Resource(name = "customerService")
	private CustomerService customerService;
	private CustomerInfo customerInfo;
	private CustomerInfo customerInfoQuery;
	private String customerId;
	public Pagination<?> page;

	Gson gson = CommonUtil.gson();

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String list() {
		if (customerInfoQuery == null) {
			customerInfoQuery = new CustomerInfo();
			customerInfoQuery.setPageSize(Constants.INIT_PAGESIZE);
			customerInfoQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			page = customerService.queryPage(customerInfoQuery);
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error("查询信息异常", e);
		}

		return LIST;
	}

	/**
	 * 客户详情
	 * 
	 * @return
	 */
	public String detail() {
		try {
			customerInfo = customerService.queryByPrimartKey(customerId);
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error("查询客户详情异常", e);
		}
		return "detail";
	}

	/**
	 * 锁定客户登录
	 * 
	 * @return
	 */
	public String lock() {
		customerInfo = new CustomerInfo();
		customerInfo.setCustomerId(customerId);
		customerInfo.setStatus(Constants.LOCK);
		int re = 0;
		try {
			re = customerService.update(customerInfo);
			redirectionUrl = "customer!list.htm";
			if (re > 0) {
				return SUCCESS;
			} else {
				addActionError("客户状态锁定失败");
			}
		} catch (Exception e) {
			addActionError("客户状态锁定失败");
			log.error("客户状态锁定失败", e);
		}
		return SUCCESS;
	}

	/**
	 * 解除锁定
	 * 
	 * @return
	 */
	public String unlock() {
		customerInfo = new CustomerInfo();
		customerInfo.setCustomerId(customerId);
		customerInfo.setStatus(Constants.OFFLINE);
		int re = 0;
		try {
			re = customerService.update(customerInfo);
			redirectionUrl = "customer!list.htm";
			if (re > 0) {
				return SUCCESS;
			} else {
				addActionError("客户状态解除锁定失败");
			}
		} catch (Exception e) {
			addActionError("客户状态解除锁定失败");
			log.error("客户状态解除锁定失败", e);
		}
		return SUCCESS;
	}

	public CustomerInfo getCustomerInfo() {
		return customerInfo;
	}

	public void setCustomerInfo(CustomerInfo customerInfo) {
		this.customerInfo = customerInfo;
	}

	public CustomerInfo getCustomerInfoQuery() {
		return customerInfoQuery;
	}

	public void setCustomerInfoQuery(CustomerInfo customerInfoQuery) {
		this.customerInfoQuery = customerInfoQuery;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

}
